﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using acessaDB;
using Frame;
using System.Data;
using System.Configuration;
using FirebirdSql.Data.FirebirdClient;
using Estrutura;


namespace Banco
{

    public class BD_Formula_contador
    {
        #region variaveis_globais

        acessaDB.conectaDBFireBird fireBird = new conectaDBFireBird();
        string conectionString = System.Configuration.ConfigurationManager.AppSettings["FireBirdConnectionLocal"];
        string usuario = string.Empty;

        #endregion

        #region construtor

        public BD_Formula_contador()
        {
            if (System.Configuration.ConfigurationManager.AppSettings["ambiente"] == "PROD")
                conectionString = System.Configuration.ConfigurationManager.AppSettings["FireBirdConnectionProd"];
        }
        public BD_Formula_contador(string usu_Login)
        {
            if (System.Configuration.ConfigurationManager.AppSettings["ambiente"] == "PROD")
                conectionString = System.Configuration.ConfigurationManager.AppSettings["FireBirdConnectionProd"];
            usuario = usu_Login;
        }

        #endregion

        #region metodos

        public DataSet _retorna_Formula_contador(
                                                string CTM_FORMULA_ID,
                                                string CFM_FORMULA,
                                                string CFM_ATIVO,
                                                string CMF_DESCRICAO
                                                )
        {

            if (string.IsNullOrEmpty(CTM_FORMULA_ID)) CTM_FORMULA_ID = "'0'";
            if (string.IsNullOrEmpty(CFM_FORMULA)) CFM_FORMULA = "''";
            if (string.IsNullOrEmpty(CFM_ATIVO)) CFM_ATIVO = "''";
            if (string.IsNullOrEmpty(CMF_DESCRICAO)) CMF_DESCRICAO = "''";

            string sql = "SELECT " +
                                  "CTM_FORMULA.CTM_FORMULA_ID, " +
                                  "CTM_FORMULA.CFM_FORMULA, " +
                                  "CTM_FORMULA.CFM_ATIVO, " +
                                  "CTM_FORMULA.CMF_DESCRICAO " +
                            "FROM CTM_FORMULA " +
                            "WHERE " +
                            "      (CTM_FORMULA.CTM_FORMULA_ID                  = " + CTM_FORMULA_ID + "  OR " + CTM_FORMULA_ID + " = '0') AND " +
                            "      (CTM_FORMULA.CFM_FORMULA                     = " + CFM_FORMULA + "  OR " + CFM_FORMULA + " = '') AND " +
                            "      (CTM_FORMULA.CFM_ATIVO                       = " + CFM_ATIVO + "  OR " + CFM_ATIVO + " = '') AND " +
                            "      (CTM_FORMULA.CMF_DESCRICAO                   = " + CMF_DESCRICAO + "  OR " + CMF_DESCRICAO + " = '') ";

            DataSet dsRetorno = fireBird.retornaDataSet(sql, conectionString);
            return dsRetorno;

        }


        public List<ES_Formula_contador> _obter_Formula_contador(
                                                string CTM_FORMULA_ID,
                                                string CFM_FORMULA,
                                                string CFM_ATIVO,
                                                string CMF_DESCRICAO
                                                )
        {

            if (string.IsNullOrEmpty(CTM_FORMULA_ID)) CTM_FORMULA_ID = "'0'";
            if (string.IsNullOrEmpty(CFM_FORMULA)) CFM_FORMULA = "''"; else CFM_FORMULA = Util.retonarAspas(CFM_FORMULA + " % ");
            if (string.IsNullOrEmpty(CFM_ATIVO)) CFM_ATIVO = "''"; else CFM_ATIVO = Util.retonarAspas(CFM_ATIVO + " % ");
            if (string.IsNullOrEmpty(CMF_DESCRICAO)) CMF_DESCRICAO = "''"; else CMF_DESCRICAO = Util.retonarAspas(CMF_DESCRICAO + " % ");

            string sql = "SELECT " +
                                  "CTM_FORMULA.CTM_FORMULA_ID, " +
                                  "CTM_FORMULA.CFM_FORMULA, " +
                                  "CTM_FORMULA.CFM_ATIVO, " +
                                  "CTM_FORMULA.CMF_DESCRICAO " +
                            "FROM CTM_FORMULA " +
                            "WHERE " +
                            "      (CTM_FORMULA.CTM_FORMULA_ID                  = " + CTM_FORMULA_ID + "  OR " + CTM_FORMULA_ID + " = '0') AND " +
                            "      (CTM_FORMULA.CFM_FORMULA                     = " + CFM_FORMULA + "  OR " + CFM_FORMULA + " = '') AND " +
                            "      (CTM_FORMULA.CFM_ATIVO                       = " + CFM_ATIVO + "  OR " + CFM_ATIVO + " = '') AND " +
                            "      (CTM_FORMULA.CMF_DESCRICAO                   = " + CMF_DESCRICAO + "  OR " + CMF_DESCRICAO + " = '') "+
                            " ORDER BY CTM_FORMULA.CMF_DESCRICAO ";

            List<ES_Formula_contador> lista = new List<ES_Formula_contador>();
            FbConnection fbConexao = new FbConnection(conectionString);
            FbDataReader dr = fireBird.retornaDataReader(sql, conectionString, fbConexao);
            try
            {
                while (dr.Read())
                {
                    ES_Formula_contador item = new ES_Formula_contador();
                    item.CTM_FORMULA_ID = dr["CTM_FORMULA_ID"].ToString();
                    item.CFM_FORMULA = dr["CFM_FORMULA"].ToString();
                    item.CFM_ATIVO = dr["CFM_ATIVO"].ToString();
                    item.CMF_DESCRICAO = dr["CMF_DESCRICAO"].ToString();
                    lista.Add(item);

                }
                return lista;
            }
            finally
            {
                fireBird.fecharConexao(fbConexao);
            }
        }


        public ES_Formula_contador _obter_Formula_contador_by_id(
                                                string CTM_FORMULA_ID
                                                )
        {

            if (string.IsNullOrEmpty(CTM_FORMULA_ID)) CTM_FORMULA_ID = "'0'";

            string sql = "SELECT " +
                                  "CTM_FORMULA.CTM_FORMULA_ID, " +
                                  "CTM_FORMULA.CFM_FORMULA, " +
                                  "CTM_FORMULA.CFM_ATIVO, " +
                                  "CTM_FORMULA.CMF_DESCRICAO " +
                            "FROM CTM_FORMULA " +
                            "WHERE " +
                            "      (CTM_FORMULA.CTM_FORMULA_ID = " + CTM_FORMULA_ID + "  OR " + CTM_FORMULA_ID + " = '0') ";

            ES_Formula_contador item = new ES_Formula_contador();
            FbConnection fbConexao = new FbConnection(conectionString);
            FbDataReader dr = fireBird.retornaDataReader(sql, conectionString, fbConexao);
            try
            {
                while (dr.Read())
                {
                    item.CTM_FORMULA_ID = dr["CTM_FORMULA_ID"].ToString();
                    item.CFM_FORMULA = dr["CFM_FORMULA"].ToString();
                    item.CFM_ATIVO = dr["CFM_ATIVO"].ToString();
                    item.CMF_DESCRICAO = dr["CMF_DESCRICAO"].ToString();

                }
                return item;
            }
            finally
            {
                fireBird.fecharConexao(fbConexao);
            }
        }


        public void _inserir_Formula_contador(
                                                string CTM_FORMULA_ID,
                                                string CFM_FORMULA,
                                                string CFM_ATIVO,
                                                string CMF_DESCRICAO
                                                )
        {

            string sql = " INSERT INTO CTM_FORMULA " +
                         "      ( " +
                         "          CFM_FORMULA                    , " +
                         "          CFM_ATIVO                      , " +
                         "          CMF_DESCRICAO                    " +
                 "      )" +
                 "  VALUES " +
                 "      ( " +
                              Util.retonarAspas(CFM_FORMULA, true) + ", " +
                              Util.retonarAspas(CFM_ATIVO, true) + ", " +
                              Util.retonarAspas(CMF_DESCRICAO, true) +
                 "      )";
            try
            {
                fireBird._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        public void _atualizar_Formula_contador(
                                                string CTM_FORMULA_ID,
                                                string CFM_FORMULA,
                                                string CFM_ATIVO,
                                                string CMF_DESCRICAO
                                                )
        {

            string sql = " UPDATE CTM_FORMULA SET " +
                         "      CFM_FORMULA                     = " + Util.retonarAspas(CFM_FORMULA, true) + ", " +
                         "      CFM_ATIVO                       = " + Util.retonarAspas(CFM_ATIVO, true) + ", " +
                         "      CMF_DESCRICAO                   = " + Util.retonarAspas(CMF_DESCRICAO, true) +
                         " WHERE CTM_FORMULA_ID =  " + CTM_FORMULA_ID;
            try
            {
                fireBird._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        public void _excluir_Formula_contador(string CTM_FORMULA_ID)
        {
            string sql = "DELETE FROM CTM_FORMULA WHERE CTM_FORMULA_ID = " + CTM_FORMULA_ID;
            try
            {
                fireBird._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        #endregion
    }
}
